package org.rzp.ideaplug.statistic.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import org.rzp.ideaplug.statistic.R;
import org.rzp.ideaplug.statistic.dialog.ProjectStatisticInfoDialog;
import org.rzp.ideaplug.statistic.statistic.StatisticThread;

import java.io.File;

public class ProjecStatistisAction extends AnAction {

    private static final String FILE_TITLE = "PsiDirectory:";

    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getProject();
        R.project = project;
        R.statistic = null;

        Object[] selectedItems = e.getData(PlatformDataKeys.SELECTED_ITEMS);
        if (null != selectedItems) {
            Object selected = selectedItems[0];
            if (selected.toString().startsWith(FILE_TITLE)) {
                R.selectedPath = selected.toString().substring(FILE_TITLE.length());
                R.selectedFile = new File(R.selectedPath);
            } else
                return;
        } else
            return;

        ProjectStatisticInfoDialog dialog = new ProjectStatisticInfoDialog(project, true);
        StatisticThread statisticThread = new StatisticThread(dialog.getJProgressBar(), dialog.getJProcessText(), dialog.getJTextAreaStatistic());
        new Thread(statisticThread).start();
        dialog.show();

    }
}
